package com.windgo.service;

import com.windgo.bean.wxVo.DefaultKeywordEntity;
import com.windgo.bean.wxVo.HistoryKeywordListEntity;
import com.windgo.bean.wxVo.HotKeywordListEntity;
import com.windgo.bean.wxVo.WxSearchVo;
import com.windgo.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zlh
 * @since 2022/10/14 17:07
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    /**
     * @author zlh
     * wx-search-index
     *
     * @return
     */
    @Override
    public WxSearchVo searchIndex() {
        DefaultKeywordEntity defaultKeywordEntity = null;
        List<HistoryKeywordListEntity> historyKeywordListEntityList = null;
        List<HotKeywordListEntity> hotKeywordListEntities = null;
        try {
            defaultKeywordEntity = marketSearchHistoryMapper.defaultKeyWord();
            historyKeywordListEntityList = marketSearchHistoryMapper.historyKeyWords();
            hotKeywordListEntities = marketSearchHistoryMapper.selectHotKeyword();
        } catch (Exception e) {
            e.printStackTrace();
        }
        WxSearchVo wxSearchVo = new WxSearchVo();
        wxSearchVo.setDefaultKeyword(defaultKeywordEntity);
        wxSearchVo.setHistoryKeywordList(historyKeywordListEntityList);
        wxSearchVo.setHotKeywordList(hotKeywordListEntities);
        return wxSearchVo;
    }

    /**
     * wx-search-helper
     * @author zlh
     * @param keyword
     * @return
     */
    @Override
    public List<String> searchKeyWord(String keyword) {
        List<String> strings = marketSearchHistoryMapper.selectKeyWord(keyword);
        return strings;
    }

    /**
     * 删除历史搜索痕迹
     * @author zlh
     * @return
     */
    @Override
    public int clearSearchHistory() {
        try {
            marketSearchHistoryMapper.deletAllKeyWords();
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 404;
        }
    }
}
